/*
 * Copyright (C) 2012 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.entities;

import inventory.*;
import inventory.RedKey;
import java.awt.image.BufferedImage;
import rpg.Game;
import rpg.Location;
import rpg.Maps.Map;
import rpg.gfx.Bitmap;
import rpg.util.ImageUtil;

/**
 * rothens.tumblr.com
 * @author Rothens
 */
public class Chest extends TileEntity {

	int animPhase;
	int type;
	Bitmap[] graphics;
	boolean opened;
	int nextPhase = 3;

	@Override
	public Bitmap getDrawGraphics() {
		return graphics[animPhase];
	}

	public Chest(Map map, int pos, int type) {
		super(new Location(pos, map));
		BufferedImage bfImg = ImageUtil.createImageIcon("/res/PurpleChest.png");
		graphics = new Bitmap[4];
		for (int i = 0; i < 4; i++) {
			graphics[i] = ImageUtil.getSub(bfImg, i, type);
		}
		this.type = type;
		animPhase = 0;
	}
	
	public boolean isOpened(){
		return opened;
	}
	
	public void open(Player p){
		if(!opened){
			switch(type){
				case 2:
					if(!p.getInventory().hasItem("key_blue")){
						return;
					}
					p.getInventory().consume("key_blue");
					p.getInventory().addItem(new GreenKey());
					break;
				case 1:
					if(!p.getInventory().hasItem("key_green")){
						return;
					}
					p.getInventory().consume("key_green");
					break;
				case 3:
					if(!p.getInventory().hasItem("key_red")){
						return;
					}
					p.getInventory().consume("key_red");
					p.getInventory().addItem(new BlueKey());
					break;
				case 0:
					p.getInventory().addItem(new RedKey());
					break;
			}
			opened = true;
			Game.eh.addToActive(this);
		}
	}

	public boolean tick() {
		nextPhase --;
		if (nextPhase <= 0) {
			nextPhase = 3;
			if (animPhase < graphics.length) {
				animPhase++;
			}
		}
		return animPhase < graphics.length - 1;
	}
	
	@Override
	public boolean isPassable(){
		return false;
	}
}
